<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<md-step label="Subscriptions" md-complete="$ctrl.parent.steps[2].completed">
  <md-step-body>
    <div layout="column" layout-align="space-around center" class="layout-padding">
      <div layout="row" layout-align="space-around center" class="layout-padding" style="width: 100%;">
        <md-autocomplete md-input-id="new-view-apis-autocomplete-id"
                         flex="70"
                         md-search-text="$ctrl.filterAPI"
                         md-selected-item-change="$ctrl.onSelectAPI(api)"
                         md-items="api in $ctrl.parent.apis | filter:$ctrl.filterAPI"
                         md-item-text="api.name"
                         placeholder="Select an API to subscribe"
                         md-autofocus="false"
                         md-min-length="0"
                         md-no-cache="true"
                         md-menu-class="autocomplete-custom-template">
          <md-item-template>
            <md-list-item class="md-2-line contact-item">
              <div class="md-list-item-text" style="margin: 15px 0;">
                <h3>{{api.name}} <small>{{api.version}}</small></h3>
              </div>
            </md-list-item>
          </md-item-template>
          <md-not-found>
            No API matching "{{$ctrl.filterAPI}}" were found.
          </md-not-found>
        </md-autocomplete>
      </div>

      <div class="gv-forms" style="width: 100%;">
        <div class="gv-form">
          <div class="gv-form-content">
            <gravitee-empty-state ng-if="$ctrl.parent.selectedAPIs.length === 0 && !$ctrl.selectedAPI"
                                  icon="dashboard"
                                  model="API"
                                  message="No API selected for your application"></gravitee-empty-state>

            <div layout="row" layout-wrap class="inset" ng-if="$ctrl.parent.selectedAPIs.length > 0 && !$ctrl.selectedAPI">
              <md-table-container style="width: 100%; min-height: 200px; text-align: center;">
                <table md-table>
                  <thead md-head md-order="query.order">
                  <tr md-row>
                    <th md-column width="1%" md-order-by="name"><span>Name</span></th>
                    <th md-column width="1%">Plans</th>
                    <th md-column width="1%"></th>
                  </tr>
                  </thead>
                  <tbody md-body>
                  <tr md-row ng-repeat="api in $ctrl.getSelectedAPIs() track by api.id">
                    <td md-cell nowrap>
                      <gravitee-identity-picture 
                        image-id="api.id"
                        image-url="::api.picture_url"
                        image-name="api.name + '  ' + api.version"
                        image-width="40"
                      ></gravitee-identity-picture>
                      {{api.name}}
                      ({{api.version}})
                    </td>
                    <td md-cell>{{api.plans}}</td>
                    <td md-cell>
                      <md-button class="md-icon-button" aria-label="Edit Plans" ng-click="$ctrl.onSelectAPI(api);">
                        <ng-md-icon icon="settings"></ng-md-icon>
                        <md-tooltip md-direction="left">Edit Plans</md-tooltip>
                      </md-button>
                    </td>
                  </tr>
                  </tbody>
                </table>
              </md-table-container>
            </div>

            <div ng-if="$ctrl.plans && $ctrl.selectedAPI" layout="column" flex layout-align="center" class="layout-padding" style="overflow: auto; align-items: center">
              <div style="width: 100%;">
                <div flex layout="row">
                  <h4>Choose <b>Echo</b> plans that suit your business</h4>
                  <md-tooltip>Close the API</md-tooltip>
                  <span flex></span>
                  <md-button class="md-icon-button" aria-label="Finish">
                    <ng-md-icon icon="close" ng-click="$ctrl.selectedAPI = null; $ctrl.filterAPI = '';"></ng-md-icon>
                  </md-button>
                </div>
                <md-divider></md-divider>
                <div layout="row" layout-align="center" class="layout-padding" flex layout-wrap>
                  <api-plan ng-repeat="plan in $ctrl.plans" plan="plan" subscribable="true" unsubscribable="true" on-subscribe="$ctrl.parent.onSubscribe($ctrl.selectedAPI, plan)" on-unsubscribe="$ctrl.parent.onUnsubscribe($ctrl.selectedAPI, plan)"></api-plan>
                  <h3 ng-if="$ctrl.plans.length === 0">There is no published plan for this API that you can subscribe with this type of application</h3>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <md-step-actions layout="row">
      <div flex>
        <md-button ng-click="$ctrl.parent.previous()">PREVIOUS</md-button>
      </div>
      <div flex layout="row" layout-align="end top">
        <md-button ng-click="$ctrl.parent.next()" ng-disabled="$ctrl.parent.selectedPlans.length">SKIP</md-button>
        <md-button ng-click="$ctrl.parent.next()" ng-disabled="!$ctrl.parent.selectedPlans.length">NEXT</md-button>
      </div>
    </md-step-actions>
  </md-step-body>
</md-step>
